package com.kayt.cyberBar.common.business.job.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kayt.cyberBar.common.FairCode;
import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.business.job.service.IOrderJobServer;
import com.kayt.cyberBar.common.cache.PublicCache;
import com.kayt.cyberBar.common.utils.StringMathUtils;
import com.kayt.cyberBar.common.utils.TimeUtils;
import com.kayt.cyberBar.company.business.dao.ICompanyBaseOrderDao;
import com.kayt.cyberBar.company.business.entity.po.BCompanyBaseOrder;
import com.kayt.cyberBar.company.business.entity.vo.CompanyOrderQueryVO;
import com.kayt.cyberBar.company.business.server.ICompanyBaseOrderServer;
import com.kayt.cyberBar.cpublic.business.entity.po.CPublicKey;

@Service("iOrderJobServer")
public class OrderJobServerImpl implements IOrderJobServer {
	private static final Logger logger = LoggerFactory.getLogger(OrderJobServerImpl.class);
	private static String PUBLIC_KEY = "transitTime";
	
	@Autowired
	private PublicCache publicCache;
	
	@Autowired
	private ICompanyBaseOrderDao iCompanyBaseOrderDao;

	@Autowired
	private ICompanyBaseOrderServer iCompanyBaseOrderServer;
	
	@Override
	public void execute () {
		try {
			List<CPublicKey> keyList = this.publicCache.getPublicValue(PUBLIC_KEY);
			String transitTime = "24";
			if (keyList != null && !keyList.isEmpty()) {
				transitTime = keyList.get(0) == null ? "24" : keyList.get(0).getParamValue();
			}
			CompanyOrderQueryVO query = new CompanyOrderQueryVO();
			query.setEndTime(TimeUtils.generateTimeStr());
			Long confirmTime = new Date().getTime() - Math.round(Double.parseDouble(transitTime) * 60 * 60 * 1000) - 24 * 60 * 60 * 1000;
			Long confirmEndTime = new Date().getTime() - Math.round(Double.parseDouble(transitTime) * 60 * 60 * 1000);
			query.setConfirmTime(TimeUtils.generateTimeStrByTimeMill(confirmTime));
			query.setConfirmEndTime(TimeUtils.generateTimeStrByTimeMill(confirmEndTime));
			PageData<BCompanyBaseOrder> orderData = this.iCompanyBaseOrderServer.get_order_list(query);
			if (orderData != null) {
				List<BCompanyBaseOrder> orderList = orderData.getPageList();
				if (orderList != null && orderList.size() > 0) {
					for (BCompanyBaseOrder order : orderList) {
						if (order != null && order.getStatusCode() != null && 
								(order.getStatusCode().equals(this.publicCache.getFairCode(FairCode.SENDING.getCode()))
								|| order.getStatusCode().equals(this.publicCache.getFairCode(FairCode.TAKED.getCode())))) {
							Long remainPrice = StringMathUtils.isNumber(order.getRemainPrice()) ? Long.parseLong(order.getRemainPrice()) : 0L;
							if (0L == remainPrice) {
								order.setInBound("1");
//								order.setStatusCode(this.publicCache.getFairCode(FairCode.DONE.getCode()));
							} else {
								order.setInBound("-1");
//								order.setStatusCode(this.publicCache.getFairCode(FairCode.NOTFAIR.getCode()));
							}
							order.setUpdateTime(TimeUtils.generateTimeStr());
							this.iCompanyBaseOrderDao.updateOrder(order);
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("excute OrderJobServerImpl with a SQLException ", e);
		}
	}
}
